package com.ruipengkj.model;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public class RadioButtonColumn extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor, ActionListener
{
	JTable table;
	JRadioButton renderButton;
	JRadioButton editButton;
	String text;
	public RadioButtonColumn(JTable table, int column)
	{
		super();
		this.table = table;
		renderButton = new JRadioButton();
		editButton = new JRadioButton();
		editButton.setFocusPainted( false );
		editButton.addActionListener( this );
		TableColumnModel columnModel = table.getColumnModel();
		System.out.print(columnModel);
		columnModel.getColumn(column).setCellRenderer(this);
		columnModel.getColumn(column).setCellEditor(this);
	}
	public Component getTableCellRendererComponent(
	    JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
	{
	    if (hasFocus)
	    {
	        //renderButton.setForeground(table.getForeground());
	        renderButton.setForeground(Color.BLUE);
	        renderButton.setBackground(table.getBackground());
	    }
	    else if (isSelected)
	    {
	        if(renderButton.isSelected())
	        {
	            renderButton.setSelected(false);
	        }
	        else
	        {
	            renderButton.setSelected(true);
	        }
	        renderButton.setForeground(table.getSelectionForeground());
	         renderButton.setBackground(table.getSelectionBackground());
	    }
	    else
	    {
	        renderButton.setForeground(table.getForeground());
	        renderButton.setBackground(table.getBackground());
	    }
	    renderButton.setText( (value == null) ? this.text : value.toString() );
	    return renderButton;
	}
	public Component getTableCellEditorComponent(
	    JTable table, Object value, boolean isSelected, int row, int column)
	{
	    text = (value == null) ? this.text : value.toString();
	    editButton.setText( text );
	    return editButton;
	}
	public Object getCellEditorValue()
	{
	    return text;
	}
	public void actionPerformed(ActionEvent e)
	{
	    fireEditingStopped();
	    System.out.println( e.getActionCommand() + " : " + table.getSelectedRow());
	}
	
	@Override
	public void addCellEditorListener(CellEditorListener arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void cancelCellEditing() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean isCellEditable(EventObject arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public void removeCellEditorListener(CellEditorListener arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean shouldSelectCell(EventObject arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean stopCellEditing() {
		// TODO Auto-generated method stub
		return false;
	}
	public static void main(String args[]) {
		JFrame frame = new JFrame();
		JTable table = new JTable(new DefaultTableModel(new String[][]{{"a", "b"},{"a", "b"},{"a", "b"}}, new String[]{"1", "2"}));
		new RadioButtonColumn(new JTable(), 1);
		frame.add(table);
		frame.setVisible(true); 
	} 
}
